*******************************
*
* Cleaning the data for ReStats Manuscript
*
*******************************


cap log "Loanshark_clean.log", replace

use borrower_illegal_anonymized_new.dta, clear

*** Generate dummies from variables indicating "Why choose this lender"
forvalues i = 1/22 {
	gen whythisahlongn`i' = 0
		foreach j in a b c d e f g h {
			quietly replace whythisahlongn`i'=1 if whythisahlong_`j'==`i'
		}
		quietly replace whythisahlongn`i'=. if whythisahlong_a==.
	}

lab var whythisahlongn7 "Why this lender: referred by a trusted friend"
lab var whythisahlongn2 "Why this lender: Willing to loan the amount"
lab var whythisahlongn8 "Why this lender: Longer term/allow installment"
lab var whythisahlongn9 "Why this lender: borrow history"
lab var whythisahlongn3 "Why this lender: require fewer IDs"
lab var whythisahlongn1 "Why this lender: offers the best rate" 
lab var whythisahlongn4 "Why this lender: less physical violence"

*** Generate dummies from variables indicating "Under which conditon, borrower borrows from lender"
forvalues i = 1/7 {
	gen conditn`i' = 0
		foreach j in a b c  {
			quietly replace conditn`i'=1 if condit_`j'==`i'
		}
		quietly replace conditn`i'=. if condit_a==.
	}

lab var conditn4 "Borrow condition: under gambling"
lab var conditn1 "Borrow condition: under normal condition"
lab var conditn2 "Borrow condition: after drinking"
lab var conditn5 "Borrow condition: under threat" 
lab var conditn3 "Borrow condition: under drug"

*** Generate dummies from variables indicating "Reason why borrower borrows from lender"

forvalues i = 1/27 {
	gen reasonn`i' = 0
		foreach j in a b c d e f {
			quietly replace reasonn`i'=1 if reason_`j'==`i'
		}
		quietly replace reasonn`i'=. if reason_a==.&reason_b==.&reason_c==.&reason_d==.
	}

lab var reasonn5 "Why borrow: for gambling "
lab var reasonn4 "Why borrow: for buying drinks/drugs"
lab var reasonn14 "Why borrow: for paying lender debt" 
lab var reasonn9  "Why borrow: for paying bills"
lab var reasonn6  "Why borrow: for treating friends"
lab var reasonn12 "Why borrow: for paying gambling debt" 
lab var reasonn10 "Why borrow: for entertainment/women"

*** Generate dummies from variables indicating "Screen documents required by lender"
forvalues i = 1/8 {
	gen collateraln`i' = 0
		foreach j in a b c d {
			quietly replace collateraln`i'=1 if collateral_`j'==`i'
		}
		quietly replace collateraln`i'=. if collateral_a==.&collateral_b==.&collateral_c==.&collateral_d==.
	}
	
lab var collateraln2 "Screen docs: personal ID"
lab var collateraln4 "Screen docs: friends ID"
lab var collateraln7 "Screen docs: Singpass"
lab var collateraln8 "Screen docs: friends/family contact No."

*** Generate dummies from variables indicating "how borrower repays the loan"

forvalues i = 1/29 {
	gen howrepayn`i' = 0
		foreach j in a b c d e {
			quietly replace howrepayn`i'=1 if howrepay_`j'==`i'
		}
		quietly replace howrepayn`i'=. if howrepay_a==.&howrepay_b==.&howrepay_e==.&howrepay_f==.
		quietly replace howrepayn`i'=. if howrepay_a==0 | howrepay_b==0 |howrepay_e==0|howrepay_f==0

	}	
sum howrepayn*

lab var howrepayn1 "Repay by using income"
lab var howrepayn4 "Repay by borrowing from another lender"
lab var howrepayn2 "Repay by borrowing from friends"
lab var howrepayn3 "Repay by borrowing from family"
lab var howrepayn6 "Repay by gambling winnings"
lab var howrepayn9 "Repay by selling valuables"

*** Generate dummies from variables indicating "Types of harrassment inflicted on borrower"
	
replace force_a= 0 if force_a==. & force_b==. & force_c==. & force_d==. & force_e==.& force_f==. & force_g==.
replace force_a = . if force_a==22|force_a==9999

forvalues i = 0/21 {
	gen forcen`i' = 0
		foreach j in a b c d e f g {
			quietly replace forcen`i'=1 if force_`j'==`i'
		}
	}
	
forvalues i = 1/8 {
	gen workforahlongn`i' = 0
		foreach j in a b c  {
			quietly replace workforahlongn`i'=1 if workforahlong_`j'==`i'
		}
		quietly replace workforahlongn`i'=. if workforahlong_a==.&workforahlong_b==.
	}

gen remind = 1*(forcen1==1|forcen11==1|forcen12==1) if force_a!=.|force_b!=.
gen personal = 1*(forcen16==1|forcen19==1) if force_a!=.|force_b!=.
gen face = 1*(forcen2==1|forcen3==1|forcen4==1|forcen6==1|forcen7==1) if force_a!=.|force_b!=.
gen vandal = 1*(forcen5==1|forcen13==1|forcen14==1|forcen15==1|forcen17==1|forcen20==1) if force_a!=.|force_b!=.
gen useid = forcen9

gen noforce = 1
forvalues i = 1/21 {
	replace noforce = 0 if forcen`i'==1
	}
	
replace noforce = . if force_a==.&force_b==.
lab var noforce "No Harassment"
lab var remind "Reminder Call "

lab var forcen11 "Harassment: Phone call"
lab var forcen1  "Harassment: verbal threat"

lab var forcen2 "Harassment: Shout at you in your neighborhood"
lab var forcen3 "Harassment: Stalking you in public venue and shout at you"
lab var forcen4 "Harassment: Harass you in your workplace" 
lab var forcen6 "Harassment: Harass family members/friends"
lab var forcen7 "Harassment: Harass neighbors" 
lab var forcen16 "Harassment: Knock doors/gate" 
lab var forcen17 "Harassment: Chain door/gate/block key holes/put superclue on key holes" 
lab var forcen18 "Harassment: Visiting home"
lab var forcen19 "Harassment: Visiting workplace"
lab var forcen12 "Harassment: Demand letter/send note/threatening message" 
lab var forcen0  "Harassment: Nothing"

lab var forcen5  "Harassment: Splash Paint/Kerosene in your building"
lab var forcen13 "Harassment: Throw flowerpot" 
lab var forcen14 "Harassment: Scribble wall"
lab var forcen15 "Harassment: Graffiti" 
lab var forcen20 "Harassment: Scratch & splash paint on car"
******** Generate and Label ID related variables ********

sort id loanno
* merge with loanshark names file after cleaning 
merge 1:1 id loanno using revname
lab var sharkid5 "Lender ID"

* create borrower-lender pair tag
egen shark5_bor = group(sharkid5 id)
lab var shark5_bor "Borrower-lender pair"
* create borrower tag
egen id_tag=tag(id)
lab var id_tag "Borrower ID tag"


******** Adding few more new variables ********

gen default = 1*(repayfull==0|repayend<2) if repayfull*repayend!=. 
lab var  default "Default"

gen latefee_share = (totalpaid-loan*(1+rate))/totalpaid if default==1
lab var  latefee_share "Late Fee Paid as a Share of Loan"

gen lendrate=loan/loanasked	
lab var lendrate "Percent of Loan Asked Lent"

gen rate_pct = rate*100
lab var rate_pct "Nominal Rate"


gen lenderrequestwork = .
replace lenderrequestwork = 1 if (workforahlong_no==0 & default)
replace lenderrequestwork = 0 if (workforahlong_no==1 & default)
lab var  lenderrequestwork "Lender requested work for him"

gen conscription = .
replace conscription = 1 if (workforahlong_no == 0 & refuse ==0 & default)
replace conscription = 0 if (workforahlong_no == 0 & refuse ==1 & default)
lab var conscription "Request accepted if lender requested borrower to work for him"


lab var repayendD "Finally Repay (Dummy)"
lab var loanasked "Loan Sought"
lab var borrowfreq  "Frequency of Borrowing Money from Loan Shark on an average year"
lab var totalpaid "Total Paid"


save loanshark_data_final.dta, replace





